package com.hanson.common.util;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class WeiXinMsgUtil {
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WxMaService wxMaService;

    /**
     * 发送小程序订阅消息
     * 订阅消息字段格式：https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
     *
     * @param toOpenId
     * @param templateId
     * @param pageUrl    小程序地址
     * @param paramObj
     */
    public void sendMaMsg(String toOpenId, String templateId, String pageUrl, JSONObject paramObj) {
        log.info("发送小程序订阅消息，toOpenId={} templateId={}", toOpenId, templateId);
        WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
        //跳转小程序页面路径
        subscribeMessage.setPage(pageUrl);
        //模板消息id
        subscribeMessage.setTemplateId(templateId);
        //给谁推送 用户的openid （可以调用根据code换openid接口)
        subscribeMessage.setToUser(toOpenId);
        //创建一个参数集合
        List<WxMaSubscribeMessage.Data> wxMaSubscribeDataList = new ArrayList<>();
        // 添加模板数据
        for (Map.Entry<String, Object> entry : paramObj.entrySet()) {
            wxMaSubscribeDataList.add(new WxMaSubscribeMessage.Data(entry.getKey(), String.valueOf(entry.getValue())));
        }
        subscribeMessage.setData(wxMaSubscribeDataList);
        try {
            //进行推送
            wxMaService.getMsgService().sendSubscribeMsg(subscribeMessage);
            log.info("发送小程序订阅消息成功 toOpenId={} templateId={}", toOpenId, templateId);
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("发送小程序订阅消息失败", e);
        }
    }

    /**
     * 发送公众号模板消息
     *
     * @param toOpenId
     * @param templateId
     * @param paramObj
     */
    public void sendMpMsg(String toOpenId, String templateId, String jumpUrl, JSONObject paramObj) {
        log.info("发送公众号模板消息，toOpenId={} templateId={}", toOpenId, templateId);
        // 发送模板消息接口
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                // 接收者openid
                .toUser(toOpenId)
                // 模板id
                .templateId(templateId)
                // 模板跳转链接
                .url(jumpUrl)
                .build();
        // 添加模板数据
        for (Map.Entry<String, Object> entry : paramObj.entrySet()) {
            templateMessage.addData(new WxMpTemplateData(entry.getKey(), String.valueOf(entry.getValue())));
        }
        String msgId = null;
        try {
            // 发送模板消息
            msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("发送公众号模板消息 result = {}", msgId);
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("发送公众号模板消息失败", e);
        }
    }
}
